﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CBMS.Interfaces;
using CBMS.Business;
using CBMS.Models;
using CBMS.Objects;
using System.Dynamic;
using CBMS.View;
using CBMS.Uc;
namespace CBMS.Common
{
    public class CommonFunction
    {
        #region Default Param 
        public static string FORMAT_DATE = "dd/MM/yyyy";
        #endregion


        public static IMasterRepository masterRes { get; set; }
        public static IRolesRepository roleRes { get; set; }
        public static IBranchRepository branchRes { get; set; }
        public static ICountryRepository countryRes { get; set; }
        public static IStateRepository stateRes { get; set; }
        public static ISubCategoryRepository subRes { get; set; }
        public static ISystemConfigRepository systemConfigbRes { get; set; }
        public static DateTime Now
        {
            get
            {
                return DateTime.Now;
            }
        }

        public static void MakeComboBoxMaster(string categoryId, ComboBox idControlCmb)
        {
            masterRes = new MasterRepository();
            var lstSubCategory = masterRes.GetAllSubCategroy(categoryId);
            ItemDTO firstItem = new ItemDTO(BaseFunctions.DrpValueNull, BaseFunctions.DrpDisplayNull);
            lstSubCategory.Insert(0, firstItem);
            idControlCmb.DataSource = lstSubCategory;
            idControlCmb.DisplayMember = "Name";
            idControlCmb.ValueMember = "Id";
        }
        public static void MakeComboBoxMaster(string categoryId, ComboBox idControlCmb, bool addNullItem)
        {
            masterRes = new MasterRepository();
            var lstSubCategory = masterRes.GetAllSubCategroy(categoryId);
            if (addNullItem)
            {
                ItemDTO firstItem = new ItemDTO(BaseFunctions.DrpValueNull, BaseFunctions.DrpDisplayNull);
                lstSubCategory.Insert(0, firstItem);
            }
            idControlCmb.DataSource = lstSubCategory;
            idControlCmb.DisplayMember = "Name";
            idControlCmb.ValueMember = "Id";
        }
        public static void MakeComboBoxRole(ComboBox idControlCmb, bool addNullItem)
        {
            roleRes = new RolesRepository();
            var lstDate = roleRes.GetAllToComboBox();
            if (addNullItem)
            {
                ItemDTO firstItem = new ItemDTO(BaseFunctions.DrpValueNull, BaseFunctions.DrpDisplayNull);
                lstDate.Insert(0, firstItem);
            }
            idControlCmb.DataSource = lstDate;
            idControlCmb.DisplayMember = "Name";
            idControlCmb.ValueMember = "Id";
        }
        public static void MakeCheckBoxMaster(string categoryId, CheckedListBox cbl)
        {
            masterRes = new MasterRepository();
            var lstSubCategory = masterRes.GetAllSubCategroy(categoryId);
            cbl.DataSource = lstSubCategory;
            cbl.DisplayMember = "Name";
            cbl.ValueMember = "Id";
        }
        public static void MakeCheckBoxAbility(string categoryId, CheckedListBox cbl)
        {
            subRes = new SubCategoryRepository();
            var lstSubCategory = subRes.GetAllAbilityByCategory(categoryId);
            cbl.DataSource = lstSubCategory;
            cbl.DisplayMember = "Name";
            cbl.ValueMember = "Id";
        }
        public static void MakeComboBoxBranch(ComboBox idControlCmb, bool addNullItem)
        {
            branchRes = new BranchRepository();
            var lstDate = branchRes.GetAllToComboBox();
            if (addNullItem)
            {
                ItemDTO firstItem = new ItemDTO(BaseFunctions.DrpValueNull, BaseFunctions.DrpDisplayNull);           
                lstDate.Insert(0, firstItem);
            }
            idControlCmb.DataSource = lstDate;
            idControlCmb.DisplayMember = "Name";
            idControlCmb.ValueMember = "Id";
        }

        public static void MakeComboBoxCountry(ComboBox idControlCmb, bool addNullItem)
        {
            countryRes = new CountryRepository();
            var lstSubCategory = countryRes.GetAll();
            if (addNullItem)
            {
                Country firstItem = new Country();
                firstItem.country_code = BaseFunctions.DrpValueNull;
                firstItem.country_name = BaseFunctions.DrpDisplayNull;
                // new ItemDTO(BaseFunctions.DrpValueNull, BaseFunctions.DrpDisplayNull);

                lstSubCategory.Insert(0, firstItem);
            }
            idControlCmb.DataSource = lstSubCategory;
            idControlCmb.DisplayMember = "country_name";
            idControlCmb.ValueMember = "country_code";
        }
        public static void MakeComboBoxState(ComboBox idControlCmb,string countryCode, bool addNullItem)
        {
            stateRes = new StateRepository();
            var lstSubCategory = stateRes.GetAllByCountry(countryCode);
            if (addNullItem)
            {
                State firstItem = new State();
                firstItem.state_code = BaseFunctions.DrpValueNull;
                firstItem.state_name = BaseFunctions.DrpDisplayNull;
                // new ItemDTO(BaseFunctions.DrpValueNull, BaseFunctions.DrpDisplayNull);
                lstSubCategory.Insert(0, firstItem);
            }
            idControlCmb.DataSource = lstSubCategory;
            idControlCmb.DisplayMember = "state_name";
            idControlCmb.ValueMember = "state_code";
        }

        public static void SetFormBeforeShow(frmBase formShow)
        {
            formShow.StartPosition = FormStartPosition.CenterParent;
            formShow.MaximizeBox = false;
            formShow.MinimizeBox = false;
            formShow.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        }
        public static void ShowForm(frmBase formShow,frmBase baseForm)
        {
            SetFormBeforeShow(formShow);
            formShow.userLogin = baseForm.userLogin;
            formShow.userLoginBranch = baseForm.userLoginBranch;
            formShow.userLoginId = baseForm.userLoginId;
            formShow.userLoginRole = baseForm.userLoginRole;
            formShow.ShowDialog();
        }
        public static void ShowForm(frmBase formShow, BaseControl baseCtr)
        {
            SetFormBeforeShow(formShow);
            formShow.userLogin = baseCtr.userLogin;
            formShow.userLoginBranch = baseCtr.userLoginBranch;
            formShow.userLoginId = baseCtr.userLoginId;
            formShow.userLoginRole = baseCtr.userLoginRole;
            formShow.ShowDialog();
        }
        public static void SetLoginData(BaseControl control,frmBase fromBase)
        {
            control.userLogin = fromBase.userLogin;
            control.userLoginBranch = fromBase.userLoginBranch;
            control.userLoginId = fromBase.userLoginId;
            control.userLoginRole = fromBase.userLoginRole;            
        }

        public static int GetDefaultPageSize()
        {
            int pageDefault = 20;
            systemConfigbRes = new SystemConfigRepository();
            var objSystemConfig = systemConfigbRes.GetSystemConfig();
            if (objSystemConfig != null)
            {
                pageDefault = objSystemConfig.page_size.Value;
            }
            return pageDefault;
        }
    }
}
